package com.itljm.cxrMarket.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itljm.cxrMarket.common.PageResult;
import com.itljm.cxrMarket.common.Result;
import com.itljm.cxrMarket.entity.HygienePenalties;
import com.itljm.cxrMarket.service.HygienePenaltiesService;
import io.micrometer.common.util.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author ljm
 * @date 2025/5/21 16:51
 */
@RestController
@RequestMapping("/hygienePenalties")
public class HygienePenaltiesController {
    @Autowired
    private HygienePenaltiesService hygienePenaltiesService;

    // 分页查询
    @RequestMapping("/info")
    public PageResult<HygienePenalties> findHygienePenaltiesByPage(
            @RequestParam(required = false, defaultValue = "0") Integer page,
            @RequestParam(required = false, defaultValue = "15") Integer limit,
            @RequestParam(required = false) String merchantId,
            @RequestParam(required = false) String penaltyType
    ) {
        Page<HygienePenalties> infoPage = new Page<>(page, limit);
        QueryWrapper<HygienePenalties> wrapper = new QueryWrapper<>();

        if (Strings.isNotEmpty(merchantId)) {
            wrapper.eq("hp.merchant_id", merchantId);
        }
        if (StringUtils.isNotEmpty(penaltyType)) {
            wrapper.eq("hp.penalty_type", penaltyType);
        }

        List<HygienePenalties> list = hygienePenaltiesService.findPenaltiesByPage(infoPage, wrapper);
        return new PageResult<>(list, infoPage.getTotal());
    }

    // 查询单个处罚记录
    @RequestMapping("/{id}")
    public Result findHygieneViolationsById(@PathVariable Integer id) {
        HygienePenalties penalties = hygienePenaltiesService.findHygienePenaltiesById(id);
        if (penalties != null) {
            return Result.ok(penalties);
        } else {
            return Result.fail("违规记录不存在");
        }
    }

    // 添加处罚记录
    @PostMapping
    public Result addHygienePenalties(@RequestBody HygienePenalties hygienePenalties) {
        System.out.println(hygienePenalties.toString());
        hygienePenalties.setIssueTime(new Date());
        hygienePenalties.setStatus(0);
        hygienePenalties.setCreateTime(new Date());
        hygienePenalties.setUpdateTime(new Date());
        return hygienePenaltiesService.save(hygienePenalties)?Result.ok():Result.fail();
    }

    // 删除处罚记录
    @DeleteMapping("/{id}")
    public Result deleteHygienePenalties(@PathVariable Integer id) {
        return hygienePenaltiesService.removeById(id)?Result.ok():Result.fail();
    }

    // 修改处罚记录
    @PutMapping
    public Result updateHygienePenalties(@RequestBody HygienePenalties hygienePenalties) {
        hygienePenalties.setUpdateTime(new Date());
        return hygienePenaltiesService.updateById(hygienePenalties)?Result.ok():Result.fail();
    }

}
